package com.xdja;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.Unpooled;
import io.netty.buffer.UnpooledDirectByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.proxy.HttpProxyHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.DefaultThreadFactory;
import io.netty.util.concurrent.EventExecutorGroup;
import io.netty.util.concurrent.Promise;

import java.net.InetSocketAddress;
import java.util.Timer;
import java.util.TimerTask;

import static io.netty.buffer.Unpooled.EMPTY_BUFFER;

/**
 * shadowsockets 代协议
 *
 * @author zk
 * @since 2018/7/27
 */
public class ServerStart {
    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup bossEventLoopGroup = new NioEventLoopGroup();
        EventLoopGroup worksEventLoopGroup = new NioEventLoopGroup();
        ServerBootstrap serverBootstrap = new ServerBootstrap();

        ChannelFuture channelFuture = serverBootstrap.group(bossEventLoopGroup, worksEventLoopGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_KEEPALIVE, false)
                .childHandler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel ch) throws Exception {
                        ch.pipeline()
                                .addLast(new HttpServerCodec())
                                .addLast(new SimpleChannelInboundHandler<HttpRequest>() {
                            @Override
                            protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
                                HttpResponse httpResponse;
                                if(msg.method() == HttpMethod.CONNECT && msg.uri().contains("127")){
                                    httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, EMPTY_BUFFER);
                                } else {
                                    httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, ctx.alloc().buffer().writeBytes("ok".getBytes()));
                                }
                                httpResponse.headers().set("transfer-encoding", "trunked");
                                httpResponse.headers().set("content-type", "text/html;charset=utf-8");
                                ctx.channel().writeAndFlush(httpResponse);
                            }
                        });
                    }
                }).bind(1081).sync();
        channelFuture.channel().closeFuture().sync();
    }
}
